#include "PluginInfoWidget.h"
#include "ui_PluginInfoWidget.h"
#include <QFont>
#include <QTextEdit>
#include <math.h>

PluginInfoWidget::PluginInfoWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PluginInfoWidget)
{
    ui->setupUi(this);
    init();
}

PluginInfoWidget::~PluginInfoWidget()
{
    delete ui;
    delete errorWidget;
}

void PluginInfoWidget::setPluginJsonData(const PluginJsonData& data)
{
    QString pluginName = QString("%1(%2)").arg(data.pluginNameCN,data.pluginName);
    ui->_pluginName->setText(pluginName);
#if defined(_WIN32) || defined(_WIN64)
    ui->_appName->setText(data.appName + ".exe");
#else
    ui->_appName->setText(data.appName);
#endif

    ui->_status->setText("正常");
    ui->progressBar->setValue(0);
    ui->pushButton->setHidden(true);
    _is_status_normal = true;

    const static QString infoStyle = "QProgressBar::chunk{background:#0000ff}";
    //ui->progressBar->setStyleSheet(infoStyle);
}

void PluginInfoWidget::setPluginJsonErrorData(PluginJsonErrorData&& data)
{
    ui->_status->setText("<font color=\"#FF0000\">异常结束</font>");
    ui->pushButton->setHidden(false);
    _is_status_normal = false;

    const static QString warnStyle = "QProgressBar::chunk{background:#ff0000}";
    //ui->progressBar->setStyleSheet(warnStyle);

    _errorData = data;
}

void PluginInfoWidget::updateProgressValue(int value)
{
    if(_is_status_normal){
        ui->progressBar->setValue(value);
    }
}

void PluginInfoWidget::init()
{
    ui->_pluginName->clear();
    ui->_status->clear();
    ui->progressBar->setValue(0);
    ui->pushButton->setHidden(true);
    _is_status_normal = true;
}

void PluginInfoWidget::on_pushButton_clicked()
{
    if(!errorWidget){
        errorWidget = new QWidget;
        errorWidget->resize(1200,600);
        errorWidget->setWindowTitle("异常信息");
        errorEdit = new QTextEdit;
        QFont font;
        font.setPointSize(16);
        errorEdit->setFont(font);
        errorEdit->setReadOnly(true);
        QHBoxLayout* layout = new QHBoxLayout(errorWidget);
        layout->setContentsMargins(0,0,0,0);
        layout->addWidget(errorEdit);
        errorWidget->setLayout(layout);
    }
    errorWidget->activateWindow();
    errorWidget->show();

    errorEdit->clear();
    errorEdit->append(QString("插件名称：%1").arg(ui->_pluginName->text()));
    errorEdit->append(QString("程序名称：%1").arg(ui->_appName->text()));
    errorEdit->append(QString("异常信息："));
    errorEdit->append(QString("　　　类型：%1").arg(_errorData.type));
    errorEdit->append(QString("　　　文件：%1").arg(_errorData.file));
    errorEdit->append(QString("　　　函数：%1").arg(_errorData.func));
    errorEdit->append(QString("　　　行号：%1").arg(_errorData.line));
    errorEdit->append(QString("　　　信息：%1").arg(_errorData.msg));
}

bool PluginInfoWidget::is_status_normal() const
{
    return _is_status_normal;
}

